package com.wuliu.action.sys;

import java.io.IOException;
import java.util.List;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import com.wuliu.business.sys.MenuObj;
import com.wuliu.business.sys.UserObj;
import com.wuliu.model.sys.Menu;
import com.wuliu.model.sys.User;
import com.wuliu.util.BaseUtil;
import com.wuliu.action.BaseAction;
import com.wuliu.util.DbUtil;

public class MenuAction extends BaseAction {
	public String menuManage(){
		return "menuManage";
	}
	public String getMenuTree() {
		String sql = "SELECT MENU_ID,MENU_NAME,PARENT_ID FROM WL_MENU ";
		List<List<String>> list = DbUtil.getQueryList(sql, 3);
		
		
		
		JSONArray j = new JSONArray();
		for(List<String> lt:list){
			JSONObject json = new JSONObject();
//			System.out.println("sss");
            json.put("id", BaseUtil.getStr(lt.get(0)));
            json.put("name", BaseUtil.getStr(lt.get(1)));
            json.put("pId", BaseUtil.getStr(lt.get(2)));
            j.add(json);
		}
		try {
			response.setCharacterEncoding("utf8");
			response.getWriter().print(j);
		} catch (IOException e) {			
			e.printStackTrace();
		}	
		return null;
	}
	
	 // 添加、修改菜单 
    public String saveMenuAjax(){

        String parentId =BaseUtil.getStr(request.getParameter("parentId"));
        String menuId = BaseUtil.getStr(request.getParameter("menuId"));
        String menuName =BaseUtil.getStr(request.getParameter("menuName"));
        String menuImg =BaseUtil.getStr(request.getParameter("menuImg"));
        String menuRequest =BaseUtil.getStr(request.getParameter("menuRequest"));
        String sortNum =BaseUtil.getStr(request.getParameter("sortNum"));

        menuRequest = menuRequest.trim();
        menuRequest = menuRequest.replaceAll("@", "&");
        
        Menu menu = null;
        MenuObj obj = new MenuObj();
        menu = obj.getMenuById(menuId);
        
        menu.setActiveFlag("0");
        

        menu.setMenuName(menuName);
        menu.setMenuRequest(menuRequest);
        menu.setSortNum(sortNum); 
        menu.setMenuImg(menuImg);
        menu.setParentId(parentId); 

        int i = 0;
        if (menuId==null||menuId.equals("")) { 
        	menu.setAddBy(((User)session.get("user")).getUserAccount());
        	i = obj.addMenu(menu);
        } else {
            menu.setUpdBy(((User)session.get("user")).getUserAccount());
        	i = obj.updateMenu(menu);
        }
        String msg = "保存错误"; 
        if (i>0) {
            msg = "保存成功";
        }
        
        try {
        	response.setCharacterEncoding("utf8");
            response.getWriter().write(msg);
        } catch(Exception ee) {
            System.out.println(ee);
        }
        return null;
    }
	// 删除菜单 
    public String delMenuAjax(){
        String menuId = BaseUtil.getStr(request.getParameter("menuId"));
        String msg = new MenuObj().deleteMenu(menuId);

        try {
        	response.setCharacterEncoding("utf8");
            response.getWriter().write(msg);
        } catch(Exception ee) {
            System.out.println(ee);
        }
        return null;
    }
    public String getMenuInfo(){
    	String id	= BaseUtil.getStr(request.getParameter("id"));
    	Menu menu = MenuObj.getMenuById(id);

    	JSONObject json = JSONObject.fromObject(menu);
    	try {
			response.setCharacterEncoding("utf8");
			response.getWriter().print(json.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return null;
    }
}
